package com.yzxb.messystem.service.impl;

import com.yzxb.messystem.entity.ProductionRecord;
import com.yzxb.messystem.entity.WorkOrder;
import com.yzxb.messystem.exception.ResourceNotFoundException;
import com.yzxb.messystem.mapper.ProductionRecordMapper;
import com.yzxb.messystem.service.ProductionRecordService;
import com.yzxb.messystem.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ProductionRecordServiceImpl implements ProductionRecordService {
    
    @Autowired
    private ProductionRecordMapper productionRecordMapper;
    
    @Autowired
    private WorkOrderService workOrderService;
    
    @Override
    public List<ProductionRecord> getAllProductionRecords() {
        return productionRecordMapper.selectList(null);
    }
    
    @Override
    public Optional<ProductionRecord> getProductionRecordById(Long id) {
        ProductionRecord record = productionRecordMapper.selectById(id);
        return Optional.ofNullable(record);
    }
    
    @Override
    public ProductionRecord addProductionRecord(ProductionRecord productionRecord) {
        // 设置创建时间
        productionRecord.setCreateTimeDefault();
        productionRecordMapper.insert(productionRecord);
        return productionRecord;
    }
    
    @Override
    public ProductionRecord updateProductionRecord(Long id, ProductionRecord productionRecord) {
        // 检查生产记录是否存在
        ProductionRecord existingRecord = productionRecordMapper.selectById(id);
        if (existingRecord == null) {
            throw new ResourceNotFoundException("ProductionRecord", "id", id);
        }
        
        // 更新生产记录信息
        existingRecord.setQuantity(productionRecord.getQuantity());
        existingRecord.setRecordType(productionRecord.getRecordType());
        existingRecord.setWorkstation(productionRecord.getWorkstation());
        existingRecord.setOperator(productionRecord.getOperator());
        existingRecord.setShift(productionRecord.getShift());
        existingRecord.setQualityStatus(productionRecord.getQualityStatus());
        existingRecord.setRemarks(productionRecord.getRemarks());
        // 设置更新时间
        existingRecord.setUpdateTimeDefault();
        
        productionRecordMapper.updateById(existingRecord);
        return existingRecord;
    }
    
    @Override
    public void deleteProductionRecord(Long id) {
        // 检查生产记录是否存在
        if (productionRecordMapper.selectById(id) == null) {
            throw new ResourceNotFoundException("ProductionRecord", "id", id);
        }
        productionRecordMapper.deleteById(id);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByWorkOrderId(Long workOrderId) {
        return productionRecordMapper.findByWorkOrderId(workOrderId);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByRecordType(ProductionRecord.RecordType recordType) {
        return productionRecordMapper.findByRecordType(recordType);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByWorkstation(String workstation) {
        return productionRecordMapper.findByWorkstation(workstation);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByOperator(String operator) {
        return productionRecordMapper.findByOperator(operator);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByShift(String shift) {
        return productionRecordMapper.findByShift(shift);
    }
    
    @Override
    public List<ProductionRecord> getProductionRecordsByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return productionRecordMapper.findByCreateTimeBetween(startTime, endTime);
    }
    
    @Override
    @Transactional
    public ProductionRecord addProductionQuantity(Long workOrderId, Integer quantity, String workstation, String operator, String shift) {
        // 检查工单是否存在
        WorkOrder workOrder = workOrderService.getWorkOrderById(workOrderId)
                .orElseThrow(() -> new ResourceNotFoundException("WorkOrder", "id", workOrderId));
        
        // 创建生产记录
        ProductionRecord record = new ProductionRecord();
        record.setWorkOrderId(workOrderId);
        record.setQuantity(quantity);
        record.setRecordType(ProductionRecord.RecordType.PRODUCTION);
        record.setWorkstation(workstation);
        record.setOperator(operator);
        record.setShift(shift);
        record.setQualityStatus("合格");
        // 设置创建时间
        record.setCreateTimeDefault();
        
        // 保存生产记录
        productionRecordMapper.insert(record);
        
        // 更新工单的完成数量
        Integer currentCompleted = workOrder.getCompletedQuantity() != null ? workOrder.getCompletedQuantity() : 0;
        workOrderService.updateWorkOrderQuantities(workOrderId, currentCompleted + quantity, null);
        
        return record;
    }
    
    @Override
    @Transactional
    public ProductionRecord addRejectionQuantity(Long workOrderId, Integer quantity, String workstation, String operator, String shift, String qualityStatus, String remarks) {
        // 检查工单是否存在
        WorkOrder workOrder = workOrderService.getWorkOrderById(workOrderId)
                .orElseThrow(() -> new ResourceNotFoundException("WorkOrder", "id", workOrderId));
        
        // 创建不合格记录
        ProductionRecord record = new ProductionRecord();
        record.setWorkOrderId(workOrderId);
        record.setQuantity(quantity);
        record.setRecordType(ProductionRecord.RecordType.REJECTION);
        record.setWorkstation(workstation);
        record.setOperator(operator);
        record.setShift(shift);
        record.setQualityStatus(qualityStatus);
        record.setRemarks(remarks);
        // 设置创建时间
        record.setCreateTimeDefault();
        
        // 保存不合格记录
        productionRecordMapper.insert(record);
        
        // 更新工单的不合格数量
        Integer currentRejected = workOrder.getRejectedQuantity() != null ? workOrder.getRejectedQuantity() : 0;
        workOrderService.updateWorkOrderQuantities(workOrderId, null, currentRejected + quantity);
        
        return record;
    }
}